<template>
  <div class="work-hours-container">
    <div class="header">
      <div class="left">
        <h2>我的工时统计</h2>
      </div>
      <div class="right">
        <el-date-picker
          v-model="dateFilter"
          type="month"
          placeholder="选择月份"
          format="YYYY-MM"
          value-format="YYYY-MM"
          @change="handleDateChange"
          class="date-filter"
        />
        <el-select
          v-model="statusFilter"
          placeholder="审核状态"
          clearable
          @change="handleStatusChange"
          class="status-filter"
        >
          <el-option label="待审核" value="PENDING" />
          <el-option label="已通过" value="APPROVED" />
          <el-option label="已拒绝" value="REJECTED" />
        </el-select>
      </div>
    </div>

    <el-card class="table-card">
      <el-table
        v-loading="loading"
        :data="stats"
        border
        stripe
        style="width: 100%"
      >
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="employee.position.title" label="岗位名称" min-width="150" />
        <el-table-column prop="employee.position.teacher.name" label="指导教师" width="120" />
        <el-table-column label="统计月份" width="120">
          <template #default="{ row }">
            {{ row.year }}-{{ String(row.month).padStart(2, '0') }}
          </template>
        </el-table-column>
        <el-table-column prop="totalHours" label="总工时" width="100">
          <template #default="{ row }">
            {{ row.totalHours }}小时
          </template>
        </el-table-column>
        <el-table-column prop="totalAmount" label="总金额" width="120">
          <template #default="{ row }">
            ¥{{ row.totalAmount }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="审核状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="reviewComments" label="审核意见" min-width="150" show-overflow-tooltip />
        <el-table-column label="操作" width="100" fixed="right">
          <template #default="{ row }">
            <el-button
              type="primary"
              size="small"
              @click="handleView(row)"
            >
              查看
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div v-if="!loading && (!stats || stats.length === 0)" class="empty-data">
        <el-empty description="暂无数据" />
      </div>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50]"
          layout="total, sizes, prev, pager, next"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 查看详情对话框 -->
    <el-dialog
      title="工时统计详情"
      v-model="viewDialogVisible"
      width="500px"
    >
      <el-descriptions :column="1" border>
        <el-descriptions-item label="岗位名称">
          {{ selectedStat?.employee.position.title }}
        </el-descriptions-item>
        <el-descriptions-item label="指导教师">
          {{ selectedStat?.employee.position.teacher.name }}
        </el-descriptions-item>
        <el-descriptions-item label="统计月份">
          {{ selectedStat?.year }}-{{ String(selectedStat?.month).padStart(2, '0') }}
        </el-descriptions-item>
        <el-descriptions-item label="总工时">
          {{ selectedStat?.totalHours }}小时
        </el-descriptions-item>
        <el-descriptions-item label="总金额">
          ¥{{ selectedStat?.totalAmount }}
        </el-descriptions-item>
        <el-descriptions-item label="审核状态">
          <el-tag :type="getStatusType(selectedStat?.status)">
            {{ getStatusText(selectedStat?.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item v-if="selectedStat?.reviewComments" label="审核意见">
          {{ selectedStat?.reviewComments }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getMyWorkHoursStats } from '@/api/student/workHoursStat'

// 表格数据
const loading = ref(false)
const stats = ref([])
const total = ref(0)
const currentPage = ref(1)
const pageSize = ref(10)
const statusFilter = ref('')
const dateFilter = ref('')

// 获取工时统计列表
const fetchStats = async () => {
  try {
    loading.value = true
    const [year, month] = dateFilter.value ? dateFilter.value.split('-') : []
    const response = await getMyWorkHoursStats({
      page: currentPage.value - 1,
      size: pageSize.value,
      year: year ? parseInt(year) : undefined,
      month: month ? parseInt(month) : undefined,
      status: statusFilter.value
    })
    
    if (response) {
      stats.value = response.content
      total.value = response.totalElements
    }
  } catch (error) {
    console.error('Failed to fetch stats:', error)
    ElMessage.error('获取工时统计失败')
  } finally {
    loading.value = false
  }
}

// 处理日期筛选
const handleDateChange = () => {
  currentPage.value = 1
  fetchStats()
}

// 处理状态筛选
const handleStatusChange = () => {
  currentPage.value = 1
  fetchStats()
}

// 处理分页
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchStats()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchStats()
}

// 查看详情
const viewDialogVisible = ref(false)
const selectedStat = ref(null)

const handleView = (row) => {
  selectedStat.value = row
  viewDialogVisible.value = true
}

// 获取状态类型
const getStatusType = (status) => {
  switch (status) {
    case 'PENDING':
      return 'warning'
    case 'APPROVED':
      return 'success'
    case 'REJECTED':
      return 'danger'
    default:
      return 'info'
  }
}

// 获取状态文本
const getStatusText = (status) => {
  switch (status) {
    case 'PENDING':
      return '待审核'
    case 'APPROVED':
      return '已通过'
    case 'REJECTED':
      return '已拒绝'
    default:
      return '未知'
  }
}

// 在组件挂载时获取数据
onMounted(() => {
  fetchStats()
})
</script>

<style scoped>
.work-hours-container {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.right {
  display: flex;
  gap: 16px;
}

.date-filter {
  width: 160px;
}

.status-filter {
  width: 120px;
}

.table-card {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.empty-data {
  padding: 40px 0;
  text-align: center;
}
</style> 